<?php

namespace app\common\helper\open;
use EasyWeChat\Factory;
use app\common\model\GlobalDataModel;
use app\common\logic\DataSettingLogic;
class OpenPlatformBaseHelper{
    protected  $app;
    public function __construct() {
        $DataSettingLogic = new DataSettingLogic();
        $openSetting = $DataSettingLogic->getData('open');
        $config = [
            'app_id'  =>  empty($openSetting['app_id']) ? '' : trim($openSetting['app_id']),
            'secret'  =>  empty($openSetting['app_secret']) ? '' : trim($openSetting['app_secret']),
            'token'   =>  empty($openSetting['app_token']) ? '' : trim($openSetting['app_token']),
            'aes_key' =>  empty($openSetting['app_key']) ? '' : trim($openSetting['app_key']),
            'response_type' => 'array',
            'log' => [
                'default' => 'dev', // 默认使用的 channel，生产环境可以改为下面的 prod
                'channels' => [
                    // 测试环境
                    'dev' => [
                        'driver' => 'single',
                        'path'   => runtime_path().'openplatform_'.date('Ymd').'.log',
                        'level'  => 'debug',
                    ],
                ],
            ], 
        ];
        $this->app = Factory::openPlatform($config);
        $this->_init();
    }
    
    protected function _init(){
        
    }


    protected function setTicket(){
        $ticket = cache('ticket');
        if(!$ticket){ //线上始终使用的是缓存 ，本地调用的则是数据库
             $setting = GlobalDataModel::find('ticket');
             $ticket = $setting->v;
        }
        $this->app->verify_ticket->setTicket($ticket);//本身EASYwechat有对ticket缓存
    }
    
}

